/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.test;

import java.util.Random;

import org.sss.client.DataPutter;
import org.sss.client.SssClient;
import org.sss.mapreduce.GroupID;
import org.sss.mapreduce.HashPartitioner;
import org.sss.mapreduce.Partitioner;
import org.sss.mapreduce.datatype.PackableBytes;

public class PutRandomBytes {

  public static int NRECODES  = 1000;
  public static int NLOOP     = 1000;
  public static int RECLENGTH = 8;

  public static void main(String[] args) throws Exception {
    SssClient client = new SssClient(args);

    String[] others = client.getArgs();

    boolean zero = false;

    if (others.length >= 1) {
      if (args[0].equals("-n")) {
        zero = true;
      }
    }

    Random gen = new Random();

    Class<? extends Partitioner> par = HashPartitioner.class;
    if (zero) {
      System.out.println("uses ZeroPartitioner");
      par = ZeroPartitioner.class;
    }
    GroupID table = GroupID.createRandom(client);
    DataPutter<PackableBytes, PackableBytes>  putter = 
      DataPutter.create(client, PackableBytes.class, PackableBytes.class, par, table);
    try {
      for (int i = 0;i < NRECODES;++i) {
        byte[] key   = new byte[RECLENGTH];
        byte[] value = new byte[RECLENGTH];

        gen.nextBytes(key);
        gen.nextBytes(value);

        System.out.println(String.format("put start %d/%d.", i, NLOOP));
        putter.append(new PackableBytes(key), new PackableBytes(value));
      }
    } finally {
      putter.close();
    }

    System.out.println("output data deployed - " + table.toString());
  }
}
